package com.cjl.login;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cjl.clientmvc.R;
import com.cjl.net.Client;
import com.cjl.net.Config;
import com.cjl.net.Response;
import com.cjl.net.google.HttpUrlConnectClient;
import com.cjl.net.volley.VolleyClient;
import com.cjl.utils.Constant;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "LoginActivity";

    private Client client;
    private EditText etUserName,etPassword;
    private Button bntLogin,btnRegister;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etUserName = findViewById(R.id.username);
        etPassword = findViewById(R.id.password);
        bntLogin = findViewById(R.id.login);
        btnRegister = findViewById(R.id.register);
        tvResult = findViewById(R.id.result);
        btnRegister.setOnClickListener(this);
        bntLogin.setOnClickListener(this);


        client = new Client(new VolleyClient(), new Response() {
            @Override
            public void success(Object response) {
                Log.d(TAG, "success() called with: response = [" + response + "]");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvResult.setText(response.toString());
                    }
                });
            }

            @Override
            public void fail(Object response) {
                Log.d(TAG, "fail() called with: response = [" + response + "]");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvResult.setText(response.toString());
                    }
                });
            }
        },new Config.Builder().build(),this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login:
                if (TextUtils.isEmpty(etUserName.getText()) || TextUtils.isEmpty(etPassword.getText())){
                    tvResult.setText("name or password must not be empty");
                    return;
                }
                request(Constant.LOGIN+"?account="+etUserName.getText()+"&password="+etPassword.getText());
                break;
            case R.id.register:
                if (TextUtils.isEmpty(etUserName.getText()) || TextUtils.isEmpty(etPassword.getText())){
                    tvResult.setText("name or password must not be empty");
                    return;
                }
                request(Constant.REGISTER+"?account="+etUserName.getText()+"&password="+etPassword.getText());
                break;
            default:
                break;
        }
    }

    private void request(String url){
        Log.d(TAG, "request() called with: url = [" + url + "]");
        new Thread(() -> client.request(url)).start();
    }
}